package com.kstech.gw;

import android.app.Activity;
import android.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import com.kstech.gw.fragment.HomeFragment;
import com.kstech.gw.fragment.InfoFragment;
import com.kstech.gw.fragment.MapFragment;

public class HomeActivity extends Activity {

    private DrawerLayout drawerLayout;
    private LinearLayout leftLayout;
    private RelativeLayout rightLayout;
    private MapFragment mapFragment;
    private HomeFragment homeFragment;
    private InfoFragment infoFragment;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        leftLayout=(LinearLayout) findViewById(R.id.left);
        rightLayout=(RelativeLayout) findViewById(R.id.right);

        manager = getFragmentManager();

        mapFragment = new MapFragment();
        homeFragment = new HomeFragment();
        infoFragment = new InfoFragment();

        manager.beginTransaction().add(R.id.fragment_layout,homeFragment,"home").commit();
        ListView listView=(ListView) leftLayout.findViewById(R.id.left_listview);
        listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1,new String[]{"主页","设备","信息","视频","跟帖",}));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0){

                    if(infoFragment.isVisible()) manager.beginTransaction().hide(infoFragment).commit();
                    if(mapFragment.isVisible())  manager.beginTransaction().hide(mapFragment).commit();
                    if(homeFragment.isAdded()){
                        manager.beginTransaction().show(homeFragment).commit();
                    }else {
                        manager.beginTransaction().add(R.id.fragment_layout,homeFragment,"home").commit();
                    }
                }
                if(position == 1){
                    if(homeFragment.isVisible()) manager.beginTransaction().hide(homeFragment).commit();
                    if(mapFragment.isVisible()) manager.beginTransaction().hide(mapFragment).commit();
                    if(infoFragment.isAdded()){
                        manager.beginTransaction().show(infoFragment).commit();
                    }else {
                        manager.beginTransaction().add(R.id.fragment_layout,infoFragment,"info").commit();
                    }
                }
                if(position == 2){
                    if(homeFragment.isVisible()) manager.beginTransaction().hide(homeFragment).commit();
                    if(infoFragment.isVisible()) manager.beginTransaction().hide(infoFragment).commit();
                    if(mapFragment.isAdded()){
                        manager.beginTransaction().show(mapFragment).commit();
                    }else {
                        manager.beginTransaction().add(R.id.fragment_layout, mapFragment, "map").commit();
                    }
                }
                drawerLayout.closeDrawer(leftLayout,true);
            }
        });
    }

}
